package com.beta.cust.bind.service.impl;

import com.beta.cat.utils.sequence.seq.Sequence;
import com.beta.cust.bind.config.CfgSelfRefreshBean;
import com.beta.cust.bind.config.ResultCode;
import com.beta.cust.bind.service.ICrmInterfaceService;
import com.beta.cust.bind.utils.CrmUtil;
import com.beta.cust.bind.vo.CrmCustDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: pcc
 * @Date: 2024/3/30 10:10
 * @Description:
 */
@Slf4j
@Service
public class CrmInterfaceServiceImpl implements ICrmInterfaceService {

    @Autowired
    private CfgSelfRefreshBean cfgSelfRefreshBean;

    @Autowired
    @Qualifier("smsSequence")
    private Sequence smsSequence;
    @Autowired
    @Qualifier("detailSequence")
    private Sequence detailSequence;

    @Override
    public String sendSms(String phone, String text) {
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("bsnNo", cfgSelfRefreshBean.getCrmSmsBsnNo());
            paramMap.put("reqBrcNo", cfgSelfRefreshBean.getCrmSmsReqBrcNo());
            paramMap.put("authNo", cfgSelfRefreshBean.getCrmSmsAuthNo());
            paramMap.put("cmsId", cfgSelfRefreshBean.getCrmSmsCmsId());
            paramMap.put("version", cfgSelfRefreshBean.getCrmSmsVersion());
            paramMap.put("csmSeqNo", "S" + smsSequence.nextValue());
            paramMap.put("mobile", phone);
            paramMap.put("text", text);
            String documentStr = CrmUtil.buildSmsDocStr(paramMap);
            return CrmUtil.sendSms(documentStr, cfgSelfRefreshBean.getCrmSmsSvcId(), cfgSelfRefreshBean.getCrmSmsUrl());
        } catch (Exception e) {
            log.error("crm-sendSms error:", e);
        }
        return ResultCode.ERROR_CODE_2107.getMsg();
    }

    @Override
    public CrmCustDetailVO queryDetail(String custPhone, String managerPhone) {
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("cmsId", cfgSelfRefreshBean.getCrmDetailCmsId());
            paramMap.put("version", cfgSelfRefreshBean.getCrmDetailVersion());
            paramMap.put("csmSeqNo", "D" + detailSequence.nextValue());
            paramMap.put("svcId", cfgSelfRefreshBean.getCrmDetailSvcId());
            paramMap.put("mgrPhone", managerPhone);
            paramMap.put("cstPhone", custPhone);
            String documentStr = CrmUtil.buildDetailDocStr(paramMap);
            return CrmUtil.queryDetailFromCrm(documentStr, cfgSelfRefreshBean.getCrmDetailSvcId(), cfgSelfRefreshBean.getCrmDetailUrl());
        } catch (Exception e) {
            log.error("crm-queryDetail custPhone:{},mamangerPhone:{} error:", custPhone, managerPhone, e);
        }
        return null;
    }
}
